﻿using AutoMapper.Configuration;
using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories.Dapper;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.EntityFrameworkCore
{
    public class DapperDbContextRepository : DapperRepository<TianShiProjectDbContext>, ITransientDependency
    {
        public DapperDbContextRepository(IDbContextProvider<TianShiProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        /// <summary>
        /// 查询
        /// </summary>
        public virtual async Task<List<T>> QueryAsync<T>(string sql, object param = null) where T : class, new()
        {
            var dbConnection = await GetDbConnectionAsync();
            return (await dbConnection.QueryAsync<T>(sql, param, transaction: await GetDbTransactionAsync()))
                .ToList();
        }
        /// <summary>
        /// 添加,删除,修改
        /// </summary>
        public virtual async Task<int> ExecuteAsync(string sql, object param)
        {
            var dbConnection = await GetDbConnectionAsync();
            return await dbConnection.ExecuteAsync(sql, param,
                 await GetDbTransactionAsync());
        }

        /// <summary>
        /// 首行首列
        /// </summary>
        public virtual async Task<object> ExecuteScalarAsync(string sql,object param)
        {
            var dbConnection = await GetDbConnectionAsync();
            return await dbConnection.ExecuteScalarAsync(sql,param,await GetDbTransactionAsync());
        }
    }
}
